package com.li.shop.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.li.shop.domain.Dictionary;
import com.li.shop.domain.DictionaryExample;
import com.li.shop.mapper.DictionaryMapper;
import com.li.shop.service.DictionaryService;
import com.li.shop.utils.SearchDictionaryResponse;
import io.micrometer.core.instrument.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DictionaryServiceImpl implements DictionaryService {
    @Autowired
    DictionaryMapper dictionaryMapper;


    @Override
    public List<Dictionary> list() {
        DictionaryExample example=new DictionaryExample();
        example.setOrderByClause("d_id desc");
        return dictionaryMapper.selectByExample(example);
    }

    @Override
    public void add(Dictionary dictionary) {
        dictionaryMapper.insert(dictionary);
    }

    @Override
    public int delete(String dId) {
        return dictionaryMapper.deleteByPrimaryKey(dId);
    }

    @Override
    public Dictionary get(String dId) {
        return dictionaryMapper.selectByPrimaryKey(dId);
    }

    @Override
    public void update(Dictionary dictionary) {
        dictionaryMapper.updateByPrimaryKeySelective(dictionary);
    }

    @Override
    public List<Dictionary> findAll() {
        return null;
    }

    @Override
    public int saveDictionary(Dictionary dictionary) {

        if(StringUtils.isNotBlank( dictionary.getdId()))
        {
            return dictionaryMapper.updateByPrimaryKey(dictionary);
        }
        else {
            return dictionaryMapper.insert(dictionary);
        }
    }

    @Override
    public SearchDictionaryResponse listUser(Integer pageSize, Integer pageNum, String keyword) {
        PageHelper.startPage(pageNum,pageSize);
        DictionaryExample example =new DictionaryExample();
        if (StringUtils.isNotBlank(keyword)) {
            example.createCriteria().andNameLike("%"+keyword+"%");
        }
        List<Dictionary> dictionaries= dictionaryMapper.selectByExample(example);
        return  new SearchDictionaryResponse(((Page)dictionaries).getTotal(),dictionaries);

    }

    @Override
    public String updatepath(Dictionary dictionary) {
        dictionaryMapper.updateByPrimaryKeySelective(dictionary);
        return null;
    }

    @Override
    public String updatename(Dictionary dictionary) {
        dictionaryMapper.updateByPrimaryKeySelective(dictionary);
        return null;
    }


}
